package nl.infodation.emr.pricingservice.ui;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import nl.infodation.emr.pricingservice.controller.Connector;
import nl.infodation.emr.pricingservice.ui.component.IView;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.pricingservice.valuesimporter.ValuesReader;
import nl.infodation.emr.utility.Constants;
import nl.infodation.emr.utility.DateTimeUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Window.Notification;

public class ConfigurationView extends CustomComponent implements IView, ClickListener, ValueChangeListener, Upload.SucceededListener, Upload.Receiver {

	CustomLayout ConfigurationLayout;
	
	private Label lblAfmConfig;
	private Label lblProveaConfig;
	private ComboBox cbbDeploymentBusiness;
	private Button btnCancel;
	private Button btnSave;
	private TextField tfProveaScriptPath;
	private TextField tfProdRulePathProvea;
	private TextField tfPreProdRulePathProvea;
	private Label lblProdRulePathProvea;
	private Label lblPreProdRulePathProvea;
	private Label lblUatRulePathProvea;
	private TextField tfUatRulePathProvea;
	private Label lblRuleDeployPathProvea;
	private TextField tfProdWsAddressProvea;
	private TextField tfPreProdWsAddressProvea;
	private TextField tfUatWsAddressProvea;
	private Label lblProdWsAddressProvea;
	private Label lblPreProdWsAddressProvea;
	private Label lblUatWsAddressProvea;
	private Label lblWsAddressProvea;
	private Label label_script_afm;
	private Label lblUatWsAddressAfm;
	private Label lblProdRulePathAfm;
	private Label lblPreProdRulePathAfm;
	private Label lblUatRulePathAfm;
	private TextField tfAfmScriptPath;
	private TextField tfProdRulePathAfm;
	private TextField tfPreProdRulePathAfm;
	private TextField tfUatRulePathAfm;
	private Label lblProdWsAddressAfm;
	private TextField tfProdWsAddressAfm;
	private Label lblPreProdWsAddressAfm;
	private TextField tfPreProdWsAddressAfm;
	private TextField tfUatWsAddressAfm;
	private Label lblRuleDeployPathAfm;
	private Label lblWsAddressAfm;

	private File file;
	private Upload upload;
	private ValuesReader valuesReader = new ValuesReader();
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */

	public ConfigurationView(MainView parentView) {
		this.parentView = parentView;
		initLayout();
	}

	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(ConfigurationView.class);
	private static final EntityManagerFactory EM_FACTORY = Persistence.createEntityManagerFactory(Constants.PRICING_PERSISTENCE_UNIT);
	private Connector connector = new Connector();
	private MainView parentView;
	
	
	@Override
	public void initLayout() {
		buildMainLayout();
		addListener();
		initDeploymentBusiness();
		initPathsAndWebServices();
		makePrivileges();
		setNullRepresentation();
		setCaptionForMultiLanguage();
		this.setCompositionRoot(ConfigurationLayout);
	}
	
	private void setCaptionForMultiLanguage() {
		btnSave.setCaption(SessionData.getI18N().getMessage(Constants.SAVE_BUTTON));
		cbbDeploymentBusiness.setCaption(SessionData.getI18N().getMessage(Constants.DEPLOYMENT_BUSINESS_COMBOBOX));
		
		tfCsvFolder.setCaption(SessionData.getI18N().getMessage(Constants.CSV_FOLDER_LABEL));
		
		lblAfmConfig.setValue(SessionData.getI18N().getMessage(Constants.AFM_CONFIGURATION_PANEL));
			lblWsAddressAfm.setValue(SessionData.getI18N().getMessage(Constants.WEB_SERVICE_ADDRESS_LABEL));
				tfUatWsAddressAfm.setCaption(SessionData.getI18N().getMessage(Constants.UAT_LABEL));
				tfPreProdWsAddressAfm.setCaption(SessionData.getI18N().getMessage(Constants.PREPROD_LABEL));
				tfProdWsAddressAfm.setCaption(SessionData.getI18N().getMessage(Constants.PROD_LABEL));
			lblRuleDeployPathAfm.setValue(SessionData.getI18N().getMessage(Constants.RULE_DEPLOYMENT_PATH_LABEL));
				tfUatRulePathAfm.setCaption(SessionData.getI18N().getMessage(Constants.UAT_LABEL));
				tfPreProdRulePathAfm.setCaption(SessionData.getI18N().getMessage(Constants.PREPROD_LABEL));
				tfProdRulePathAfm.setCaption(SessionData.getI18N().getMessage(Constants.PROD_LABEL));
		lblProveaConfig.setValue(SessionData.getI18N().getMessage(Constants.PROVEA_CONFIGURATION_PANEL));
			lblWsAddressProvea.setValue(SessionData.getI18N().getMessage(Constants.WEB_SERVICE_ADDRESS_LABEL));
				tfUatWsAddressProvea.setCaption(SessionData.getI18N().getMessage(Constants.UAT_LABEL));
				tfPreProdWsAddressProvea.setCaption(SessionData.getI18N().getMessage(Constants.PREPROD_LABEL));
				tfProdWsAddressProvea.setCaption(SessionData.getI18N().getMessage(Constants.PROD_LABEL));
			lblRuleDeployPathProvea.setValue(SessionData.getI18N().getMessage(Constants.RULE_DEPLOYMENT_PATH_LABEL));
				tfUatRulePathProvea.setCaption(SessionData.getI18N().getMessage(Constants.UAT_LABEL));
				tfPreProdRulePathProvea.setCaption(SessionData.getI18N().getMessage(Constants.PREPROD_LABEL));
				tfProdRulePathProvea.setCaption(SessionData.getI18N().getMessage(Constants.PROD_LABEL));
				upload.setCaption(SessionData.getI18N().getMessage(Constants.UPLOAD_A_FILE));
				upload.setButtonCaption(SessionData.getI18N().getMessage(Constants.UPLOAD_BUTTON));
	}
	
	private void initDeploymentBusiness() {
		cbbDeploymentBusiness.setNullSelectionAllowed(false);
		cbbDeploymentBusiness.removeAllItems();
		cbbDeploymentBusiness.addItem(Constants.AFM_ROLE);
		cbbDeploymentBusiness.addItem(Constants.PROVEA_ROLE);
	}
	
	private void setNullRepresentation() {
    	tfUatRulePathAfm.setNullRepresentation("");
    	tfPreProdRulePathAfm.setNullRepresentation("");
    	tfProdRulePathAfm.setNullRepresentation("");
    	tfUatRulePathProvea.setNullRepresentation("");
    	tfPreProdRulePathProvea.setNullRepresentation("");
    	tfProdRulePathProvea.setNullRepresentation("");
    	
    	tfUatWsAddressAfm.setNullRepresentation("");
    	tfPreProdWsAddressAfm.setNullRepresentation("");
    	tfProdWsAddressAfm.setNullRepresentation("");
    	tfUatWsAddressProvea.setNullRepresentation("");
    	tfPreProdWsAddressProvea.setNullRepresentation("");
    	tfProdWsAddressProvea.setNullRepresentation("");
    	tfAfmScriptPath.setNullRepresentation("");
    	tfProveaScriptPath.setNullRepresentation("");
    	
	}
	
	private void addListener() {
		upload.addListener((Upload.SucceededListener)this);
		btnSave.addListener((ClickListener)this);
		btnCancel.addListener((ClickListener)this);
		cbbDeploymentBusiness.addListener((ValueChangeListener)this);
	}
	
	public String getBusinessChosen() {
		if (SessionData.isSuperAdmin()) {
			return (String)cbbDeploymentBusiness.getValue();
		}
		return null; 
	}
	
	private void makePrivileges() {
		if (Constants.AFM_ROLE.equals(SessionData.getRoleName())) {
			removedProveaConfig();
			ConfigurationLayout.removeComponent(cbbDeploymentBusiness);
		} else if (Constants.PROVEA_ROLE.equals(SessionData.getRoleName())) {
			removedAfmConfig();
			ConfigurationLayout.removeComponent(cbbDeploymentBusiness);
		}
	}
	
	private void initPathsAndWebServices() {
		EntityManager entityManager = null;
		try {
	    	entityManager = EM_FACTORY.createEntityManager();
	    		// Initialize rule paths
		    	tfUatRulePathAfm.setValue(connector.getConfig(Constants.UAT_RULE_PATH_AFM, entityManager));
		    	tfPreProdRulePathAfm.setValue(connector.getConfig(Constants.PREPROD_RULE_PATH_AFM, entityManager));
		    	tfProdRulePathAfm.setValue(connector.getConfig(Constants.PROD_RULE_PATH_AFM, entityManager));
		    	tfUatRulePathProvea.setValue(connector.getConfig(Constants.UAT_RULE_PATH_PROVEA, entityManager));
		    	tfPreProdRulePathProvea.setValue(connector.getConfig(Constants.PREPROD_RULE_PATH_PROVEA, entityManager));
		    	tfProdRulePathProvea.setValue(connector.getConfig(Constants.PROD_RULE_PATH_PROVEA, entityManager));
		    	
		    	// Initialize web services
		    	tfUatWsAddressAfm.setValue(connector.getConfig(Constants.UAT_WS_ADDRESS_AFM, entityManager));
		    	tfPreProdWsAddressAfm.setValue(connector.getConfig(Constants.PREPROD_WS_ADDRESS_AFM, entityManager));
		    	tfProdWsAddressAfm.setValue(connector.getConfig(Constants.PROD_WS_ADDRESS_AFM, entityManager));
		    	tfUatWsAddressProvea.setValue(connector.getConfig(Constants.UAT_WS_ADDRESS_PROVEA, entityManager));
		    	tfPreProdWsAddressProvea.setValue(connector.getConfig(Constants.PREPROD_WS_ADDRESS_PROVEA, entityManager));
		    	tfProdWsAddressProvea.setValue(connector.getConfig(Constants.PROD_WS_ADDRESS_PROVEA, entityManager));
	    	
		    	// Initialize deployment business
		    	cbbDeploymentBusiness.setValue(SessionData.getDeploymentRoleName());
		    	
		    	//init script path
		    	tfAfmScriptPath.setValue(connector.getConfig(Constants.AFM_SCRIPT_PATH, entityManager));
		    	tfProveaScriptPath.setValue(connector.getConfig(Constants.PROVEA_SCRIPT_PATH, entityManager));
		    	
		    	// Initialize CSV folder
		    	tfCsvFolder.setValue(connector.getConfig(Constants.CSV_FOLDER, entityManager));
		    	
		} finally {
			if (entityManager != null && entityManager.isOpen()) {
				entityManager.close();
			}
		}
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		final Button source = event.getButton();
		if (source == btnSave) {
			LOG.info(DateTimeUtil.getNow() + "Click save button");
			boolean saved = false;
			EntityManager entityManager = null;
			try {
		    	entityManager = EM_FACTORY.createEntityManager();
		    		// Save rule paths
		    		connector.setConfig(Constants.UAT_RULE_PATH_AFM, (String)tfUatRulePathAfm.getValue(), entityManager);
		    		connector.setConfig(Constants.PREPROD_RULE_PATH_AFM, (String)tfPreProdRulePathAfm.getValue(), entityManager);
		    		connector.setConfig(Constants.PROD_RULE_PATH_AFM, (String)tfProdRulePathAfm.getValue(), entityManager);
		    		connector.setConfig(Constants.UAT_RULE_PATH_PROVEA, (String)tfUatRulePathProvea.getValue(), entityManager);
		    		connector.setConfig(Constants.PREPROD_RULE_PATH_PROVEA, (String)tfPreProdRulePathProvea.getValue(), entityManager);
		    		connector.setConfig(Constants.PROD_RULE_PATH_PROVEA, (String)tfProdRulePathProvea.getValue(), entityManager);
			    	
			    	// Save web services
		    		connector.setConfig(Constants.UAT_WS_ADDRESS_AFM, (String)tfUatWsAddressAfm.getValue(), entityManager);
		    		connector.setConfig(Constants.PREPROD_WS_ADDRESS_AFM, (String)tfPreProdWsAddressAfm.getValue(), entityManager);
		    		connector.setConfig(Constants.PROD_WS_ADDRESS_AFM, (String)tfProdWsAddressAfm.getValue(), entityManager);
		    		connector.setConfig(Constants.UAT_WS_ADDRESS_PROVEA, (String)tfUatWsAddressProvea.getValue(), entityManager);
		    		connector.setConfig(Constants.PREPROD_WS_ADDRESS_PROVEA, (String)tfPreProdWsAddressProvea.getValue(), entityManager);
		    		connector.setConfig(Constants.PROD_WS_ADDRESS_PROVEA, (String)tfProdWsAddressProvea.getValue(), entityManager);
		    		
		    		//SAVE SCRIPT
		    		connector.setConfig(Constants.AFM_SCRIPT_PATH, (String)tfAfmScriptPath.getValue(), entityManager);
		    		connector.setConfig(Constants.PROVEA_SCRIPT_PATH, (String)tfProveaScriptPath.getValue(), entityManager);
		    		
		    		// Save CSV folder
		    		connector.setConfig(Constants.CSV_FOLDER, (String)tfCsvFolder.getValue(), entityManager);
		    		
		    		if (cbbDeploymentBusiness.getValue() != null)
		    			connector.setConfig(Constants.DEPLOYMENT_BUSINESS, (String)cbbDeploymentBusiness.getValue(), entityManager);
		    		saved = true;
		    		
			} finally {
				if (entityManager != null && entityManager.isOpen()) {
					entityManager.close();
				}
			}
			if (saved) {
				getWindow().showNotification(SessionData.getI18N().getMessage(
						Constants.SUCCESSFULLY_MESSAGE), Notification.TYPE_WARNING_MESSAGE);
			} else {
				getWindow().showNotification(SessionData.getI18N().getMessage(
						Constants.FAILED_MESSAGE), Notification.TYPE_WARNING_MESSAGE);
			}
		} else if (source == btnCancel) {
			parentView.reload();
			DomainRuleList.getActiveList().clear();
			parentView.showPromotionListView(Boolean.FALSE);
		}
	}
	
	@Override
	public void reload() {
		initPathsAndWebServices();
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		SessionData.setDeploymentRoleName((String)event.getProperty().getValue());
	}

	private TextField tfCsvFolder;
	
	@AutoGenerated
	private void buildMainLayout() {
		ConfigurationLayout = new CustomLayout("Configuration");
		buildAfmConfig();
		buildProveaConfig();
		buildControl();		
		
		lblWsAddressAfm = new Label();
		lblWsAddressAfm.setImmediate(false);
		lblWsAddressAfm.setValue("Web service address");
		ConfigurationLayout.addComponent(lblWsAddressAfm, "lblWsAddressAfm");
		
		tfCsvFolder = new TextField();
		tfCsvFolder.setImmediate(false);
		tfCsvFolder.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfCsvFolder, "lcCsvFolder");
		
		upload = new Upload("Upload a file", (Upload.Receiver)this);
		upload.setStyleName("inline_field");
		ConfigurationLayout.addComponent(upload, "lcValuesImporter");
		
		// cbbDeploymentBusiness
		cbbDeploymentBusiness = new ComboBox();
		cbbDeploymentBusiness.setImmediate(false);
		cbbDeploymentBusiness.setCaption("Deployment Business");
		cbbDeploymentBusiness.setStyleName("inline_field");
		ConfigurationLayout.addComponent(cbbDeploymentBusiness,"lcCbbDeploymentBusiness");
	}
	
	private void buildAfmConfig() {
		//Afm
		lblAfmConfig = new Label();
		lblAfmConfig.setValue("Atlas for men (Afm)");
		ConfigurationLayout.addComponent(lblAfmConfig, "lblAfmConfig");
		
		//Web service address
		lblWsAddressAfm = new Label();
		lblWsAddressAfm.setImmediate(false);
		lblWsAddressAfm.setValue("Web service address");
		ConfigurationLayout.addComponent(lblWsAddressAfm, "lblWsAddressAfm");
		
//		lblUatWsAddressAfm = new Label();
//		lblUatWsAddressAfm.setImmediate(false);
//		lblUatWsAddressAfm.setValue("UAT");
//		ConfigurationLayout.addComponent(lblUatWsAddressAfm, "lblUatWsAddressAfm");
		
		tfUatWsAddressAfm = new TextField();
		tfUatWsAddressAfm.setImmediate(false);
		tfUatWsAddressAfm.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfUatWsAddressAfm, "tfUatWsAddressAfm");
		
		tfPreProdWsAddressAfm = new TextField();
		tfPreProdWsAddressAfm.setImmediate(false);
		tfPreProdWsAddressAfm.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfPreProdWsAddressAfm, "tfPreProdWsAddressAfm");
		
		tfProdWsAddressAfm = new TextField();
		tfProdWsAddressAfm.setImmediate(false);
		tfProdWsAddressAfm.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfProdWsAddressAfm, "tfProdWsAddressAfm");
		
		//Rule deployment path
		lblRuleDeployPathAfm = new Label();
		lblRuleDeployPathAfm.setImmediate(false);
		lblRuleDeployPathAfm.setValue("Rule deployment path");
		ConfigurationLayout.addComponent(lblRuleDeployPathAfm, "lblRuleDeployPathAfm");
		
//		lblUatRulePathAfm = new Label();
//		lblUatRulePathAfm.setImmediate(false);
//		lblUatRulePathAfm.setValue("UAT");
//		ConfigurationLayout.addComponent(lblUatRulePathAfm, "lblUatRulePathAfm");
		
		tfUatRulePathAfm = new TextField();
		tfUatRulePathAfm.setImmediate(false);
		tfUatRulePathAfm.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfUatRulePathAfm, "tfUatRulePathAfm");
		
		tfPreProdRulePathAfm = new TextField();
		tfPreProdRulePathAfm.setImmediate(false);
		tfPreProdRulePathAfm.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfPreProdRulePathAfm, "tfPreProdRulePathAfm");
		
		tfProdRulePathAfm = new TextField();
		tfProdRulePathAfm.setImmediate(false);
		tfProdRulePathAfm.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfProdRulePathAfm, "tfProdRulePathAfm");
		
		tfAfmScriptPath = new TextField();
		tfAfmScriptPath.setImmediate(false);
		tfAfmScriptPath.setCaption("SCRIPT");
		tfAfmScriptPath.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfAfmScriptPath, "tfAfmScriptPath");
	}
	
	private void buildProveaConfig() {
		//Provea
		lblProveaConfig = new Label();
		lblProveaConfig.setValue("Provea");
		ConfigurationLayout.addComponent(lblProveaConfig, "lblProveaConfig");
		
		//Web service address
		lblWsAddressProvea = new Label();
		lblWsAddressProvea.setImmediate(false);
		lblWsAddressProvea.setValue("Web service address");
		ConfigurationLayout.addComponent(lblWsAddressProvea, "lblWsAddressProvea");
		
		tfUatWsAddressProvea = new TextField();
		tfUatWsAddressProvea.setImmediate(false);
		tfUatWsAddressProvea.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfUatWsAddressProvea, "tfUatWsAddressProvea");
		
		tfPreProdWsAddressProvea = new TextField();
		tfPreProdWsAddressProvea.setImmediate(false);
		tfPreProdWsAddressProvea.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfPreProdWsAddressProvea, "tfPreProdWsAddressProvea");
		
		tfProdWsAddressProvea = new TextField();
		tfProdWsAddressProvea.setImmediate(false);
		tfProdWsAddressProvea.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfProdWsAddressProvea, "tfProdWsAddressProvea");
		
		//Rule deployment path
		lblRuleDeployPathProvea = new Label();
		lblRuleDeployPathProvea.setImmediate(false);
		lblRuleDeployPathProvea.setValue("Rule deployment path");
		ConfigurationLayout.addComponent(lblRuleDeployPathProvea, "lblRuleDeployPathProvea");
		
		tfUatRulePathProvea = new TextField();
		tfUatRulePathProvea.setImmediate(false);
		tfUatRulePathProvea.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfUatRulePathProvea, "tfUatRulePathProvea");
		
		tfPreProdRulePathProvea = new TextField();
		tfPreProdRulePathProvea.setImmediate(false);
		tfPreProdRulePathProvea.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfPreProdRulePathProvea, "tfPreProdRulePathProvea");
		
		tfProdRulePathProvea = new TextField();
		tfProdRulePathProvea.setImmediate(false);
		tfProdRulePathProvea.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfProdRulePathProvea, "tfProdRulePathProvea");
		
		tfProveaScriptPath = new TextField();
		tfProveaScriptPath.setImmediate(false);
		tfProveaScriptPath.setCaption("SCRIPT");
		tfProveaScriptPath.setStyleName("inline_field");
		ConfigurationLayout.addComponent(tfProveaScriptPath, "tfProveaScriptPath");
	}
	
	private void buildControl(){
		// Control
		btnSave = new NativeButton();
		btnSave.setImmediate(true);
		btnSave.setStyleName("button_style");
		ConfigurationLayout.addComponent(btnSave, "button_save");
		
		btnCancel = new NativeButton();
		btnCancel.setImmediate(true);
		btnCancel.setStyleName("right_button");
		btnCancel.addStyleName("cancel");
		ConfigurationLayout.addComponent(btnCancel, "button_cancel");
		ConfigurationLayout.addComponent(new Label(SessionData.getI18N().getMessage(
				Constants.CANCEL_BUTTON)), "lcTipCancel");
		ConfigurationLayout.addComponent(new Label(SessionData.getI18N().getMessage(
				Constants.CONFIGURATION_BUTTON)), "lbConfiguration");
	}	
	
	private void removedAfmConfig() {
		//Afm
		ConfigurationLayout.removeComponent(lblAfmConfig);
		
		//Web service address
		ConfigurationLayout.removeComponent(lblWsAddressAfm);
		ConfigurationLayout.removeComponent(lblUatWsAddressAfm);		
		ConfigurationLayout.removeComponent(tfUatWsAddressAfm);		
		ConfigurationLayout.removeComponent(lblPreProdWsAddressAfm);		
		ConfigurationLayout.removeComponent(tfPreProdWsAddressAfm);		
		ConfigurationLayout.removeComponent(lblProdWsAddressAfm);		
		ConfigurationLayout.removeComponent(tfProdWsAddressAfm);
		
		//Rule deployment path
		ConfigurationLayout.removeComponent(lblRuleDeployPathAfm);
		ConfigurationLayout.removeComponent(lblUatRulePathAfm);
		ConfigurationLayout.removeComponent(tfUatRulePathAfm);
		ConfigurationLayout.removeComponent(lblPreProdRulePathAfm);
		ConfigurationLayout.removeComponent(tfPreProdRulePathAfm);
		ConfigurationLayout.removeComponent(lblProdRulePathAfm);
		ConfigurationLayout.removeComponent(tfProdRulePathAfm);
		ConfigurationLayout.removeComponent(label_script_afm);
		ConfigurationLayout.removeComponent(tfAfmScriptPath);
	}
	private void removedProveaConfig() {
		//Provea
		ConfigurationLayout.removeComponent(lblProveaConfig);
		
		//Web service address
		ConfigurationLayout.removeComponent(lblWsAddressProvea);
		ConfigurationLayout.removeComponent(lblUatWsAddressProvea);		
		ConfigurationLayout.removeComponent(tfUatWsAddressProvea);		
		ConfigurationLayout.removeComponent(lblPreProdWsAddressProvea);		
		ConfigurationLayout.removeComponent(tfPreProdWsAddressProvea);		
		ConfigurationLayout.removeComponent(lblProdWsAddressProvea);		
		ConfigurationLayout.removeComponent(tfProdWsAddressProvea);
		
		//Rule deployment path
		ConfigurationLayout.removeComponent(lblRuleDeployPathProvea);
		ConfigurationLayout.removeComponent(lblUatRulePathProvea);
		ConfigurationLayout.removeComponent(tfUatRulePathProvea);
		ConfigurationLayout.removeComponent(lblPreProdRulePathProvea);
		ConfigurationLayout.removeComponent(tfPreProdRulePathProvea);
		ConfigurationLayout.removeComponent(lblProdRulePathProvea);
		ConfigurationLayout.removeComponent(tfProdRulePathProvea);
		ConfigurationLayout.removeComponent(label_script_afm);
		ConfigurationLayout.removeComponent(tfProveaScriptPath);
	}

	@Override
	public void uploadSucceeded(SucceededEvent event) {
		if (null != this.file) {
			boolean imported = valuesReader.importValues(this.file);
			if (imported) {
				getWindow().showNotification(
						SessionData.getI18N().getMessage(Constants.SUCCESSFULLY_MESSAGE),
						Notification.TYPE_WARNING_MESSAGE);
			}
		}
	}

	
	
	@Override
	public OutputStream receiveUpload(String filename, String mimeType) {
		// Create upload stream
        FileOutputStream fos = null; // Stream to write to
        File file = null;
        try {
            // Open the file for writing.
            file = new File(getCsvFolder() + filename);
            fos = new FileOutputStream(file);
            this.file = file;
            
        } catch (final java.io.FileNotFoundException e) {
        	getWindow().showNotification(
					SessionData.getI18N().getMessage(Constants.FILE_NOT_EXIST) + file.getAbsolutePath(),
					Notification.TYPE_WARNING_MESSAGE);
        	return null;
        }
        
        return fos; // Return the output stream to write to
	}

	private String getCsvFolder() {
    	String folder = null;
    	EntityManager entityManager = null;
		try {
	    	entityManager = EM_FACTORY.createEntityManager();
	    	folder = connector.getConfig(Constants.CSV_FOLDER, entityManager);
		} finally {
			if (entityManager != null && entityManager.isOpen()) {
				entityManager.close();
			}
		}
		if (!folder.endsWith("/") && !folder.endsWith("\\")) {
			folder += "/";
		}
		return folder;
    }
	
}
